package com.dstz.bpm.engine.parser.flow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.core.util.AppUtil;
import com.dstz.base.core.validate.ValidateUtil;
import com.dstz.bpm.api.engine.plugin.context.BpmPluginContext;
import com.dstz.bpm.api.engine.plugin.def.BpmDef;
import com.dstz.bpm.api.engine.plugin.def.BpmPluginDef;
import com.dstz.bpm.engine.model.DefaultBpmProcessDef;
import java.util.ArrayList;
import java.util.Iterator;
import org.springframework.stereotype.Component;

@Component
public class PluginsParser extends AbsFlowParse<BpmPluginContext> {
   @Override
   public Object parseDef(DefaultBpmProcessDef bpmProcessDef, String json) {
      JSONObject plugins = JSON.parseObject(json);
      this.setDefaultPlugins(plugins);
      ArrayList<BpmPluginContext> pluginContextList = new ArrayList();
      Iterator var5 = plugins.keySet().iterator();

      while(var5.hasNext()) {
         String pluginName = (String)var5.next();
         BpmPluginContext pluginContext = (BpmPluginContext)AppUtil.getBean(pluginName + "PluginContext");
         if (pluginContext == null) {
            this.LOG.error("插件解析失败，不存在的插件：{}", pluginName + "PluginContext");
         } else {
            if (pluginContext instanceof BpmPluginContext) {
               try {
                  pluginContext.parse((JSON)plugins.get(pluginName));
                  BpmPluginDef def = pluginContext.getBpmPluginDef();
                  ValidateUtil.validate(def);
               } catch (Exception var9) {
                  this.LOG.error("插件{}解析失败:{}！", new Object[]{pluginContext.getTitle(), var9.getMessage(), var9});
               }
            }

            pluginContextList.add(pluginContext);
         }
      }

      return pluginContextList;
   }

   @Override
   public String getKey() {
      return "plugins";
   }

   @Override
   public String validate(Object o) {
      return null;
   }

   private void setDefaultPlugins(JSONObject plugins) {
      if (!plugins.containsKey("dataLog")) {
         plugins.put("dataLog", new JSONObject());
      }

   }

   @Override
   public void setDefParam(DefaultBpmProcessDef bpmProcessDef, Object object) {
      ArrayList<BpmPluginContext> pluginContextList = (ArrayList)object;
      bpmProcessDef.setPluginContextList(pluginContextList);
   }
}
